package com.itheima.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class SqlSessionUtils {
    private static SqlSessionFactory factory;

    static {
        try {
            // 加载配置文件
            InputStream is = SqlSessionUtils.class.getClassLoader().getResourceAsStream("SqlMapperConfig.xml");
            // 初始化工厂
            factory = new SqlSessionFactoryBuilder().build(is);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 获取SqlSession
    public static SqlSession getSqlSession() {
        return factory.openSession();
    }
}
